package com.ocom.human.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ocom.common.entity.human.EntityDeptInfo;
import com.ocom.common.entity.human.EntityMjKaoqin;
import com.ocom.common.entity.human.EntityPersonnelInfo;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.common.enums.MasterSlaveEnum;
import com.ocom.common.request.human.StudentsDateOnlineRequest;
import com.ocom.common.request.human.StudentsKqBydaysDetailRequest;
import com.ocom.common.utils.DateUtils;
import com.ocom.common.vo.human.KqStudentDateVo;
import com.ocom.common.vo.human.StudentInfoKqDetailVo;
import com.ocom.human.dao.IDaoMjKaoqin;
import com.ocom.human.service.MjKaoqinService;
import com.ocom.mysql.aspect.DataSourceAspect;
import icu.mhb.mybatisplus.plugln.base.service.impl.JoinServiceImpl;
import icu.mhb.mybatisplus.plugln.core.JoinLambdaWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class MjKaoqinServiceImpl extends JoinServiceImpl<IDaoMjKaoqin, EntityMjKaoqin> implements MjKaoqinService {

    @Autowired
    IDaoMjKaoqin iDaoMjKaoqin;

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB,isMaster = MasterSlaveEnum.MASTER)
    public boolean addRegionInfo(Long companyId, EntityMjKaoqin entityBizApply) {
        entityBizApply.setCreateDate(new Date());
        return this.save(entityBizApply);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB,isMaster = MasterSlaveEnum.MASTER)
    public void timerSave(Long companyId) {
        iDaoMjKaoqin.timerSave(companyId,new Date());
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB,isMaster = MasterSlaveEnum.SLAVE)
    public List<KqStudentDateVo> getRow(Long companyId, StudentsDateOnlineRequest request) {
        JoinLambdaWrapper<EntityMjKaoqin> queryWrapper =  new JoinLambdaWrapper<>(EntityMjKaoqin.class);

        queryWrapper.eq(EntityMjKaoqin::getCreateDate,request.getDate());
        queryWrapper.eq(EntityMjKaoqin::getComId,companyId);
        if(request.getDeptIds().size()>0){
            queryWrapper.in(EntityMjKaoqin::getDeptId,request.getDeptIds());
        }

        queryWrapper.select(EntityMjKaoqin::getDeptId,EntityMjKaoqin::getState,EntityMjKaoqin::getCreateTime,EntityMjKaoqin::getCreateDate);

        queryWrapper
                .leftJoin(EntityDeptInfo.class,EntityDeptInfo::getDeptId, EntityMjKaoqin::getDeptId)
                .selectAs((cb) -> {
            cb.add("count(*)","students",false);
            cb.add("dept_info.dept_name","deptName",false);
        }).end();

        queryWrapper.groupBy(EntityMjKaoqin::getDeptId,EntityMjKaoqin::getCreateTime,EntityMjKaoqin::getState);

        return this.joinList(queryWrapper,KqStudentDateVo.class);

    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB,isMaster = MasterSlaveEnum.SLAVE)
    public  PageInfo<StudentInfoKqDetailVo>  getHistoryList(Long companyId, StudentsKqBydaysDetailRequest request) {
        JoinLambdaWrapper<EntityMjKaoqin> queryWrapper =  new JoinLambdaWrapper<>(EntityMjKaoqin.class);

        queryWrapper.eq(EntityMjKaoqin::getCreateTime,request.getCreateTime());
        queryWrapper.eq(EntityMjKaoqin::getComId,companyId);
        if(request.getDeptIds()!=null && request.getDeptIds().size()>0){
            queryWrapper.in(EntityMjKaoqin::getDeptId,request.getDeptIds());
        }
        if(request.getDeptId()!=null){
            queryWrapper.eq(EntityMjKaoqin::getDeptId,request.getDeptId());
        }
        if(request.getState()!=null && request.getState()>0){
            queryWrapper.in(EntityMjKaoqin::getState,request.getState());
        }
        queryWrapper.select(EntityMjKaoqin::getComId,EntityMjKaoqin::getDeptId,EntityMjKaoqin::getState,EntityMjKaoqin::getCreateTime,EntityMjKaoqin::getCreateDate);

        queryWrapper
                .notDefaultSelectAll().leftJoin(EntityDeptInfo.class,EntityDeptInfo::getDeptId, EntityMjKaoqin::getDeptId)
                .selectAs((cb) -> {
                    cb.add("dept_info.dept_name","deptName",false);
                }).end().leftJoin(EntityPersonnelInfo.class,EntityPersonnelInfo::getPId, EntityMjKaoqin::getPId)
                .selectAs((cb) -> {
                    cb.add("personnel_info.p_id","pId",false);
                    cb.add("personnel_info.p_name","pName",false);
                    cb.add("personnel_info.p_code","pCode",false);
                    cb.add("personnel_info.p_phone","phone",false);
                    cb.add("personnel_info.p_photo_url2","photo",false);
                }).end();

        PageHelper.startPage(request.getPage(),request.getSize());
        List<StudentInfoKqDetailVo> entityFileDownloadInfos = this.joinList(queryWrapper,StudentInfoKqDetailVo.class);
        return new PageInfo<>(entityFileDownloadInfos);

    }


    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB,isMaster = MasterSlaveEnum.MASTER)
    public void timerUpdate(Long companyId) {
        /**
         * update  mj_kaoqin
         * set state=12
         * where com_id=110  and create_time ='2024-12-08 07:30:00'  and state =1
         * and p_id in  (
         *  select p_id from (  select p_id from mj_kaoqin m2 where com_id=110    and create_time ='2024-12-08 08:00:00'  and state =2  )a
         * )
         */


        QueryWrapper<EntityMjKaoqin> queryWrapper=new QueryWrapper<>();
        queryWrapper.select("id","p_id");
        queryWrapper.eq("com_id",110);
        queryWrapper.eq("create_time", DateUtils.getDate()+" 08:00:00");
        queryWrapper.eq("state",2);
        queryWrapper.eq("timer",0);
        queryWrapper.last("limit 500");
//        List<Long> list = this.listObjs(queryWrapper, o -> Long.valueOf(o.toString()));

        List<EntityMjKaoqin> list = this.list(queryWrapper);

        if(list==null || list.size()==0){
            return;
        }

        List<Long> ids = list.stream().map(EntityMjKaoqin::getId).collect(Collectors.toList());
        UpdateWrapper<EntityMjKaoqin> updateWrapper=new UpdateWrapper<>();
        updateWrapper.in("id" ,ids );
        updateWrapper.set("timer",1);
        this.update(updateWrapper);


        List<Long> pIds = list.stream().map(EntityMjKaoqin::getPId).collect(Collectors.toList());

        UpdateWrapper<EntityMjKaoqin> updateWrapper2 =new UpdateWrapper<>();
        updateWrapper2.eq("com_id",110);
        updateWrapper2.eq("create_time", DateUtils.getDate()+" 07:30:00");
        updateWrapper2.eq("state",1);
        updateWrapper2.in("p_id",pIds);
        updateWrapper2.set("state",12);
        this.update(updateWrapper2);


    }

}
